//
//  CLReadViewController.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/7/23.
//

#import "CLReadViewController.h"

@interface CLReadViewController ()

/// 阅读视图
@property (nonatomic, strong) CLReadView *readView;

@end

@implementation CLReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置阅读背景
    self.view.backgroundColor = [CLReadConfigure sharedInstance].bgColor;
    
    // 刷新阅读进度
    [self reloadProgress];
}

- (void)addSubviews {
    [super addSubviews];
    
    // 阅读使用范围
    CGRect readRect = CL_READ_RECT();
    
    // 顶部状态栏
    CLReadViewStatusTopView *topView = [[CLReadViewStatusTopView alloc]init];
    topView.bookName.text = self.readModel.bookName;
    topView.chapterName.text = self.recordModel.chapterModel.name;
    [self.view addSubview:topView];
    topView.frame = CGRectMake(CGRectGetMinX(readRect), CGRectGetMinY(readRect), readRect.size.width, CL_READ_STATUS_TOP_VIEW_HEIGHT);
    
    // 底部状态栏
    CLReadViewStatusBottomView *bottomView = [[CLReadViewStatusBottomView alloc]init];
    [self.view addSubview:bottomView];
    bottomView.frame = CGRectMake(CGRectGetMinX(readRect), CGRectGetMaxY(readRect) - CL_READ_STATUS_BOTTOM_VIEW_HEIGHT, readRect.size.width, CL_READ_STATUS_BOTTOM_VIEW_HEIGHT);
    
    // 阅读视图
    [self initReadView];
}
/// 初始化阅读视图
- (void)initReadView {
    
    // 是否为书籍首页
    if (self.recordModel.pageModel == nil || self.recordModel.pageModel.isHomePage) {
        
        self.topView.hidden = YES;
        self.bottomView.hidden = YES;
        
        CLReadHomeView *homeView = [[CLReadHomeView alloc]init];
        homeView.readModel = self.readModel;
        [self.view addSubview:homeView];
        homeView.frame = CL_READ_VIEW_RECT();
        
    }else{
        
        CLReadView *readView = [[CLReadView alloc]init];
        readView.content = self.recordModel.contentAttributedString;
        [self.view addSubview:readView];
        readView.frame = CL_READ_VIEW_RECT();
    }
}

- (void)reloadProgress {
    
    if ([CLReadConfigure sharedInstance].progressType == CLProgressTypeTotal) { // 总进度
        
        // 当前阅读进度
        
        CGFloat progress = [self CL_READ_TOTAL_PROGRESS:self.readModel recordModel:self.recordModel];
       
        // 显示进度
        self.bottomView.progress.text = CL_READ_TOTAL_PROGRESS_STRING(progress);
        
        
    }else{ // 分页进度
        
        // 显示进度
        self.bottomView.progress.text = [NSString stringWithFormat:@"%@/%@",@(self.recordModel.page + 1),@(self.recordModel.chapterModel.pageCount)];
    }
}

- (void)setRecordModel:(CLReadRecordModel *)recordModel {
    _recordModel = recordModel;
}

- (void)dealloc {
    [self.bottomView removeTimer];
}
@end
